package com.example.scribe.error;

public class ScribeError extends RuntimeException {
    private final int line;
    private final int column;
    private final String input;
    private final ErrorType errorType;
    private final String details;

    public ScribeError(ErrorType type, String input, int line, int column, String details) {
        super(formatMessage(type, input, line, column, details));
        this.errorType = type;
        this.input = input;
        this.line = line;
        this.column = column;
        this.details = details;
    }

    public ErrorType getErrorType() {
        return errorType;
    }

    public int getLine() {
        return line;
    }

    public int getColumn() {
        return column;
    }

    public String getInput() {
        return input;
    }

    public String getDetails() {
        return details;
    }

    private static String formatMessage(ErrorType type, String input, int line, int column, String details) {
        StringBuilder sb = new StringBuilder();
        sb.append(type.getMessage()).append("\n");
        sb.append("位置: 第").append(line).append("行, 第").append(column).append("列\n");
        sb.append("输入: ").append(input).append("\n");
        if (details != null && !details.isEmpty()) {
            sb.append("详细信息: ").append(details);
        }
        return sb.toString();
    }

    public static ScribeError syntaxError(String input, int line, int column, String details) {
        return new ScribeError(ErrorType.SYNTAX_ERROR, input, line, column, details);
    }

    public static ScribeError typeError(String input, int line, int column, String details) {
        return new ScribeError(ErrorType.TYPE_ERROR, input, line, column, details);
    }

    public static ScribeError parseError(String input, int line, int column, String details) {
        return new ScribeError(ErrorType.PARSE_ERROR, input, line, column, details);
    }
}